using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using InfoExpress.ESM.Domains;

namespace InfoExpress.ESM.WinApp.Popups
{
  public partial class FormClassInfoDetail00 : Form
  {
    private SafeNameClass _class;
    private TList<Teacher> _teacherList = new TList<Teacher>();
    private TList<Period> _periodByTeacherList = new TList<Period>();

    public FormClassInfoDetail00()
    {
      InitializeComponent();
    }

    public FormClassInfoDetail00(SafeNameClass selectedClass)
      : this()
    {
      _class = selectedClass;
      LoadData();
    }

    #region Event handler

    private void ripceMaTTimeTable_Popup(object sender, EventArgs e)
    {
      ucPeriodMaT.ClearCheckEditText();
      ucPeriodMaT.ClearPeriodCheck();
      ucPeriodMaT.CheckEditorEnabled = true;

      if (gvMajorTeacher.FocusedRowHandle >= 0)
      {
        int total = 0;
        Teacher teacher = gvMajorTeacher.GetRow(gvMajorTeacher.FocusedRowHandle) as Teacher;

        string condition = "Date IS NULL";
        condition += " AND TeacherId=" + teacher.AccountId;

        TList<DetailPeriod> periodList = GlobalCache.DetailPeriodService.GetPaged(
            condition, string.Empty, 0, 5000, out total);

        string code = string.Empty;
        foreach (DetailPeriod detailPeriod in periodList)
        {
          code = GlobalCache.PeriodIdMap[detailPeriod.PeriodId].Code;
          ucPeriodMaT.PeriodMap[code].Checked = true;
          ucPeriodMaT.PeriodMap[code].Text = GlobalCache.ClassIdMap[detailPeriod.ClassId].Code;
          if (detailPeriod.ClassId != _class.Id)
            ucPeriodMaT.PeriodMap[code].Enabled = false;
        }
      }
    }

    private void deOpenDateTo_EditValueChanged(object sender, EventArgs e)
    {
      LoadTeacher();
    }

    #endregion

    #region Helper methods

    private void LoadData()
    {
      if (_class != null)
      {
        lblClassCode.Text = _class.Code;
        deOpenDateFrom.EditValue = _class.OpenDate;

        LoadTeacher();
      }
    }

    private void LoadTeacher()
    {
      int total = 0;
      _teacherList.Clear();
      _periodByTeacherList.Clear();

      string fromDate = (deOpenDateFrom.EditValue != null) ? deOpenDateFrom.DateTime.ToShortDateString() : string.Empty;
      string toDate = (deOpenDateTo.EditValue != null) ? deOpenDateTo.DateTime.ToShortDateString() : string.Empty;

      string condition = "ClassId=" + _class.Id + " AND Date IS NOT NULL";
      if (!string.IsNullOrEmpty(fromDate))
        condition += " AND Date >= '" + fromDate + "'";
      if (!string.IsNullOrEmpty(toDate))
        condition += " AND Date < DateAdd(day,1,'" + toDate + "')";

      TList<DetailPeriod> periodList = GlobalCache.DetailPeriodService.GetPaged(
          condition, string.Empty, 0, 5000, out total);

      foreach (DetailPeriod period in periodList)
      {
        Teacher teacher = _teacherList.Find(TeacherColumn.AccountId, period.TeacherId);
        if (teacher == null)
        {
          Teacher newTeacher = GlobalCache.TeacherService.GetByAccountId(period.TeacherId);
          _teacherList.Add(newTeacher);

          TList<DetailPeriod> taughPeriod = 
            periodList.FindAll(DetailPeriodColumn.TeacherId, newTeacher.AccountId);

          newTeacher.TaughPeriod = taughPeriod.Count;
        }
      }

      gcMajorTeacher.DataSource = _teacherList;
    }

    #endregion
  }
}